package lab.gene.action;

import java.io.File;
//import java.io.FileInputStream;

import org.apache.commons.io.FileUtils;
import java.io.IOException;

import lab.gene.service.impl.PrimerSequenceServiceImpl;
//import lab.validate.lib.ValidateLibrary;

import com.opensymphony.xwork2.ActionSupport;

public class UploadSequenceAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private File myFile;
	private String myFileContentType;
	private String myFileFileName;
	private String destPath;
	
	public String execute()
	 {
	      /* Copy file to a safe location */
	      destPath = "C:/seq/";

	      try{
	     	 System.out.println("Src File name: " + myFile);
	     	 System.out.println("Dst File name: " + myFileFileName);
	     	    	 
	     	 File destFile  = new File(destPath, myFileFileName);
	    	 FileUtils.copyFile(myFile, destFile);
	    	 PrimerSequenceServiceImpl service = new PrimerSequenceServiceImpl();
	    	 service.updateSequence(destFile);
	  
	      }catch(IOException e){
	         e.printStackTrace();
	         return ERROR;
	      }
	      return SUCCESS;
	}
	
	public void validate() {

		if (myFile ==  null || myFileFileName == null || myFile.length() > 5242880) {
			addFieldError("error", "Please select a file");
		}
		/*
		try {
			FileInputStream is = new FileInputStream (myFile);
			String type = ValidateLibrary.getTypeByStream(is);
			if (type != "csv") {
				addFieldError("error","Please choose a valid csv file");
			}
		}	
		catch (Exception e){
			e.printStackTrace();
			addFieldError("userImage", "Invalid File!");
		}*/
	}
	
	
	public File getMyFile() {
		return myFile;
	}
	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}
	public String getMyFileContentType() {
		return myFileContentType;
	}
	public void setMyFileContentType(String myFileContentType) {
		this.myFileContentType = myFileContentType;
	}
	public String getMyFileFileName() {
		return myFileFileName;
	}
	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}
	public String getDestPath() {
		return destPath;
	}
	public void setDestPath(String destPath) {
		this.destPath = destPath;
	}

}
